package com.usian.controller;

import com.usian.config.FastDFSClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;

@RestController
public class FileController {

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @Value("${fileServerUrl}")
    private String url;


    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        String path="";
        try {
            //判断文件是否存在
            if (file == null) {
                throw new RuntimeException("文件不存在");
            }
            //获取文件的完整名称
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)) {
                throw new RuntimeException("文件不存在");
            }
            path =fastDFSClientUtil.uploadFile(file);
//            path=url+path;
            path=url.concat(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

@RequestMapping("/delImage")
public boolean delImage(String filePath){
        boolean flag=false;
        try {
            fastDFSClientUtil.delFile(filePath);
            flag=true;
        }catch (Exception e){
            flag=false;
            e.printStackTrace();
        }
        return  flag;
}


}
